package com.whfc.ms.api.fse.controller;

import com.whfc.common.enums.BindFlag;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fse.dto.FseCrawlerDTO;
import com.whfc.fse.dto.FseCrawlerDataDTO;
import com.whfc.fse.dto.FseCrawlerDataLogDTO;
import com.whfc.fse.dto.FseFvsDeviceDTO;
import com.whfc.fse.param.FseCrawlerAddParam;
import com.whfc.fse.param.FseCrawlerBindParam;
import com.whfc.fse.param.FseCrawlerEditParam;
import com.whfc.fse.param.FseFvsParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.fse.service.MsFseCrawlerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 履带吊
 *
 * @author qzexing
 * @version 1.0
 * @date 2021/10/30 16:33
 */
@RestController
@RequestMapping("/ms/api/crawler")
public class FseCrawlerController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsFseCrawlerService msFseCrawlerService;


    @GetMapping(value = "/list", params = {"pageNum", "pageSize"})
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "keyword", required = false) String keyword,
                       @RequestParam(value = "bindFlag", required = false) Integer bindFlag,
                       @RequestParam(value = "netState", required = false) Integer netState) {
        logger.info("履带吊列表,loginer:{},pageNum:{},pageSize:{},deptId:{},keyword:{}",
                user.getUsername(), pageNum, pageSize, deptId, keyword);
        PageData<FseCrawlerDTO> pageData = msFseCrawlerService.list(pageNum, pageSize, deptId, keyword, bindFlag, netState);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "bindFlag", required = false) Integer bindFlag,
                       @RequestParam(value = "netState", required = false) Integer netState) {
        logger.info("履带吊列表,loginer:{},deptId:{},bindFlag:{}, netState:{}", user.getUsername(), deptId, bindFlag, netState);
        ListData<FseCrawlerDTO> listData = msFseCrawlerService.list(deptId, bindFlag, netState);
        return ResultUtil.success(listData);
    }

    @GetMapping("/detail/{crawlerId}")
    public Result detail(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @PathVariable("crawlerId") Integer crawlerId) {
        logger.info("履带吊详情,loginer:{},crawlerId:{}", user.getUsername(), crawlerId);
        FseCrawlerDTO fseCrawlerDTO = msFseCrawlerService.detail(crawlerId);
        return ResultUtil.success(fseCrawlerDTO);
    }

    @RequiresPermissions("fse/crawler/device/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestBody @Validated FseCrawlerAddParam request) {
        logger.info("添加履带吊,loginer:{},param:{}", user.getUsername(), request.toString());
        msFseCrawlerService.add(request);
        return ResultUtil.success();
    }

    @RequiresPermissions("fse/crawler/device/edit")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestBody @Validated FseCrawlerEditParam request) {
        logger.info("编辑履带吊,loginer:{},param:{}", user.getUsername(), request.toString());
        msFseCrawlerService.edit(request);
        return ResultUtil.success();
    }

    @RequiresPermissions("fse/crawler/device/delete")
    @PostMapping("/del/{crawlerId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("crawlerId") Integer crawlerId) {
        logger.info("删除履带吊,loginer:{},crawlerId:{}", user.getUsername(), crawlerId);
        msFseCrawlerService.del(crawlerId);
        return ResultUtil.success();
    }

    @PostMapping("/bind")
    public Result bind(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestBody @Validated FseCrawlerBindParam request) {
        logger.info("履带吊绑定硬件,loginer:{},param:{}", user.getUsername(), request.toString());
        request.setBingStatus(BindFlag.BIND.getValue());
        msFseCrawlerService.bind(request);
        return ResultUtil.success();
    }

    @PostMapping("/unbind/{crawlerId}")
    public Result unbind(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @PathVariable("crawlerId") Integer crawlerId) {
        logger.info("履带吊解绑硬件，loginer:{},crawlerId:{}", user.getUsername(), crawlerId);
        FseCrawlerBindParam request = new FseCrawlerBindParam();
        request.setBingStatus(BindFlag.UNBIND.getValue());
        request.setCrawlerId(crawlerId);
        msFseCrawlerService.unbind(request);
        return ResultUtil.success();
    }

    @GetMapping("/latest")
    public Result latest(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("crawlerId") Integer crawlerId) {
        logger.info("履带吊最新数据，loginer:{},crawlerId:{}", user.getUsername(), crawlerId);
        FseCrawlerDataDTO crawlerDataDTO = msFseCrawlerService.getLatest(crawlerId);
        return ResultUtil.success(crawlerDataDTO);
    }

    @GetMapping("/anon/latest")
    public Result anonLatest(@RequestParam("crawlerId") Integer crawlerId) {
        logger.info("匿名获取履带吊最新数据, crawlerId:{}", crawlerId);
        FseCrawlerDataDTO crawlerDataDTO = msFseCrawlerService.getLatest(crawlerId);
        return ResultUtil.success(crawlerDataDTO);
    }

    @GetMapping("/log")
    public Result log(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestParam("crawlerId") Integer crawlerId,
                      @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                      @RequestParam(value = "field", required = false) String field) {
        logger.info("履带吊历史数据，loginer:{},crawlerId:{}, time:{},field:{}", user.getUsername(), crawlerId, date, field);
        List<FseCrawlerDataLogDTO> list = msFseCrawlerService.getCrawlerLog(crawlerId, date, field);
        return ResultUtil.success(new ListData<>(list));
    }

    @PostMapping("/bind/fvs")
    public Result bindFvs(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestBody @Validated FseFvsParam request) {
        logger.info("塔机绑定监控设备，loginer:{},request:{}", user.getUsername(), request.toString());
        msFseCrawlerService.bindFvs(request);
        return ResultUtil.success();
    }

    @GetMapping("/fvs/list")
    public Result fvsList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("crawlerId") Integer crawlerId) {
        logger.info("获取视频监控列表，loginer:{},crawlerId:{}", user.getUsername(), crawlerId);
        List<FseFvsDeviceDTO> fvsList = msFseCrawlerService.fvsList(crawlerId);
        return ResultUtil.success(new ListData<>(fvsList));
    }

}
